package com.dong.ch03_stack_queue;

public class QueueArr<E> implements Queue<E> {

    private S03_MyArrayEnhance.Array<E> array ;

    public QueueArr() {
        this(8);
    }
    public QueueArr(int size) {
        if(size<=0) size=8;
        array=new S03_MyArrayEnhance.Array(size);
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void enqueue(E e) {
        array.addLast(e);
    }

    @Override
    public E dequeue() {
        if(isEmpty()) {
            throw  new RuntimeException("the queue is empty now");
        }
        return array.deleteFirst();
    }

    @Override
    public E getFront() {
        if(isEmpty()) {
            throw  new RuntimeException("the queue is empty now");
        }
        return array.getFirst();
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < getSize()-1; i++) {
            sb.append(array.get(i)).append(" , ");
        }
        sb.append(array.getLast());
        return "Queue  front  {"+sb+"}  tail" ;
    }

    public static void main(String[] args) {
        QueueArr<Integer> queue = new QueueArr<>();
        for (int i = 0; i < 10; i++) {
            queue.enqueue(i);
            System.out.println(queue);
            if(i%3==2) {
                Integer dequeue = queue.dequeue();
                System.out.println(queue);
            }
        }
    }
}
